package stancel.peter.smslogger;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 * Vytvara vzhlad prvkov nachadzajucich sa v liste triedy <b>MainActivity</b>.
 * @author Peter Stancel
 */
public class ListAdapter extends ArrayAdapter<Sms>{

	private List<Sms> list;
	private LayoutInflater inflater;

	/**
	 * Konstruktor.
	 * @param context Context
	 * @param list List SMS-iek.
	 */
	public ListAdapter(Context context, List<Sms> list) {
		super(context, 0, list);
		this.list = list;
		this.inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Sms getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	/**
	 * Vytvara vzhlad jednej polozky listu v <b>MainActivity</b>.
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Sms sms = list.get(position);
		View listView = inflater.inflate(R.layout.list_adapter, null);
		
		if(position % 2 == 0){
			listView.setBackgroundResource(R.drawable.background);
		}else{
			listView.setBackgroundResource(R.drawable.background2);
		}
		
		TextView tv = (TextView) listView.findViewById(R.id.text_time);
		tv.setText(sms.getTime());
		
		tv = (TextView) listView.findViewById(R.id.text_number);
		if(sms.getName() == null){
			tv.setText(sms.getPhoneNumber());
		}else{
			tv.setText(sms.getName());
		}
		
		tv = (TextView) listView.findViewById(R.id.text_text);
		tv.setText(sms.getText());
		
		return listView;
	}
}
